package job

import (
	"encoding/json"
	"git.supremind.info/product/box/vehicle/common"
	"git.supremind.info/product/box/vehicle/global"
	"git.supremind.info/product/box/vehicle/internal/dto"
	"git.supremind.info/product/box/vehicle/internal/util"
	"git.supremind.info/product/box/vehicle/internal/wsserver"
	"time"
)

// 获取车型设备状态
func GetVmrModelStatus() {
	go func() {
		for {
			vmrModelStatus := dto.VmrModelStatus{}

			// 获取车型状态信息
			url1 := global.VmrSetting.Host + "/v1/model/service/status"
			res1, err1 := util.GetData(url1)
			if err1 == nil {
				common.Logger.Infof("service/status response : %v", res1)
				vmrResponse := make(map[string]interface{})
				jsonErr1 := util.ConvByJson(res1, &vmrResponse)
				if jsonErr1 == nil {
					vmrModelStatus.DeviceStatus = vmrResponse["data"]
				}
			}

			// 获取相机设备信息
			url2 := global.VmrSetting.Host + "/v1/model/camera/status"
			res2, err2 := util.GetData(url2)
			if err2 == nil {
				common.Logger.Infof("camera/status response : %v", res2)
				vmrResponse := make(map[string]interface{})
				jsonErr2 := util.ConvByJson(res2, &vmrResponse)
				if jsonErr2 == nil {
					vmrModelStatus.CameraStatus = vmrResponse["data"]
				}
			}

			// 获取盒子信息
			url3 := global.VmrSetting.Host + "/v1/box"
			res3, err3 := util.GetData(url3)
			if err3 == nil {
				common.Logger.Infof("/v1/box response : %v", res3)
				vmrResponse := make(map[string]interface{})
				jsonErr3 := util.ConvByJson(res3, &vmrResponse)
				if jsonErr3 == nil {
					boxInfo := dto.BoxInfo{}
					util.ConvByJson(vmrResponse["data"], &boxInfo)
					vmrModelStatus.BoxInfo = boxInfo
				}
			}

			marshal, resultErr := json.Marshal(&vmrModelStatus)
			if resultErr == nil {
				wsserver.SendModelMessage("model_status", marshal)
			}

			time.Sleep(3 * time.Second) //休眠3秒
		}
	}()
}
